<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="rolePage">
        <el-row>
          <el-col :sm="4">
            <div class="g-title">早会管理</div>
          </el-col>
          <el-col :sm="20" style="text-align: right">
            <span class="role-add" @click="handleCreate()"><i class="el-icon-plus" />添加</span>
          </el-col>
        </el-row>
      </div>

      <el-date-picker
        v-model="dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        class="filter-item"
        @change="fetchData()"
      />
    </div>
    <el-row class="table">
      <el-table
        ref="multipleTable"
        :data="list"
        tooltip-effect="dark"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" label="序号" width="80" align="center" />
        <el-table-column label="日期" prop="meetingDate" show-overflow-tooltip align="center" />
        <el-table-column prop="remarks" label="备注" show-overflow-tooltip align="center" />

        <el-table-column label="操作" align="center" width="240">
          <template slot-scope="{row,$index}">
            <el-button type="primary" size="mini" @click="handleUpdate(row)">
              修改
            </el-button>
            <el-button v-if="row.status != 'deleted'" size="mini" type="danger" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="pagination">
        <el-col>
          <el-pagination
            background
            layout="total, sizes, prev, pager, next, jumper"
            :page-size="pageSize"
            :total="total"
            :current-page="pageNo"
            @current-change="fetchData"
            @size-change="pageSizeChange"
          />
        </el-col>
      </el-row>
    </el-row>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="80px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="早会日期">
          <el-date-picker
            v-model="temp.meetingDate"
            type="date"
            placeholder="选择早会日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="temp.remarks"
            :autosize="{ minRows: 2, maxRows: 4 }"
            type="textarea"
            placeholder="请填写"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          关闭
        </el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">
          {{ textMap[dialogStatus] }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import './css/index.scss'
import { meetingAdd, queryAllItems, meetingUpdate, meetingDelete } from '@/api/report'
import qs from 'qs'
export default {
  data: function() {
    return {
      activeCardName: 'all',
      dialogFormVisible: false,
      dialogStatus: '',
      // 查询

      dateRange: null,
      // 弹窗状态
      textMap: {
        update: '修改',
        create: '添加'
      },
      temp: {
        id: undefined,
        remarks: '',
        time: new Date()
      },
      rules: {
        time: [{ required: true, message: 'timestamp is required', trigger: 'change' }],
        remarks: [{ required: true, message: 'remarks is required', trigger: 'blur' }]
      },
      total: 0,
      pageNo: 1,
      pageSize: 10,
      list: [], // 全部数据
      listDataSelected: [] // 选中数据
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 重置数据
    resetTemp() {
      this.temp = {
        id: undefined,
        remarks: '',
        time: new Date()
      }
    },
    // 添加早会
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    //  修改早会
    handleUpdate(row) {
      const data = Object.assign({}, row)
      this.temp = {
        meetingId: data.meetingId,
        remarks: data.remarks,
        meetingDate: new Date(data.meetingDate)
      }
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 添加早会提交数据
    createData() {
      console.log('temp1', this.temp)
      this.$refs['dataForm'].validate(async(valid) => {
        if (valid) {
          const data = {
            meetingDate: this.getTime(this.temp.time),
            remarks: this.temp.remarks
          }
          await meetingAdd(data).then((r) => {
            console.log('temp', this.temp)
            this.dialogFormVisible = false
            if (r && r.code == 200) {
              this.$message({
                type: 'success',
                message: r.message
              })
              this.fetchData()
            } else {
              this.$message({
                type: 'error',
                message: r.message
              })
            }
          })
        }
      })
    },
    // 修改早会提交数据
    updateData() {
      this.$refs['dataForm'].validate(async(valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          await meetingUpdate(tempData).then((r) => {
            if (r && r.code == 200) {
              this.$message({
                type: 'success',
                message: r.message
              })
              this.dialogFormVisible = false
              this.fetchData()
            } else {
              this.$message({
                type: 'error',
                message: r.message
              })
            }
          })
        }
      })
    },
    // 删除早会
    async handleDelete(row) {
      await meetingDelete({ id: row.meetingId }).then((r) => {
        if (r && r.code == 200) {
          this.$message({
            type: 'success',
            message: r.message
          })
          this.fetchData()
        } else {
          this.$message({
            type: 'error',
            message: r.message
          })
        }
      })
    },
    //  查看账户
    see(data) {
      this.$router.push({
        name: 'userSee',
        query: {
          data: JSON.stringify(data)
        }
      })
    },
    // 分页组件
    pageSizeChange(newSize) {
      this.pageSize = newSize
      this.pageNo = 1
      this.fetchData()
    },
    delRole(data) {
      this.$confirm('此操作将永久删除账户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          console.log('data', data)
          const r = await deleteUser({ userId: data.userId })
          if (r && r.code == 200) {
            this.$message({
              type: 'success',
              message: r.message
            })
            this.fetchData()
          } else {
            this.$message({
              type: 'error',
              message: r.message
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async fetchData(pageNo) {
      if (pageNo) {
        this.pageNo = pageNo
      }

      const params = {
        pageNum: this.pageNo,
        pageSize: this.pageSize,

        starTime: '',
        endTime: ''
      }
      if (this.dateRange) {
        const start = this.getTime(this.dateRange[0])
        const end = this.getTime(this.dateRange[1])
        params.starTime = start
        params.endTime = end
      }
      const data = await queryAllItems(params)
      this.list = data.data.content
      this.total = data.data.totalSize
    },
    // 表格多选
    handleSelectionChange(val) {
      this.listDataSelected.splice(0)
      val.forEach(e => {
        this.listDataSelected.push(e)
      })
    },
    getTime(time) {
      var date = new Date(time)
      var y = date.getFullYear()
      var m = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
      var d = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
      return y + '-' + m + '-' + d
    }
  }
}
</script>
<style lang="scss" scoped>
.userlist {
    margin-right: 10px;
}
</style>
